package mylist;

import java.util.Arrays;

public class MyArrayList implements IList{
    public int[] elem;

    private int usedSize;//0

    //顺序表的默认大小
    private static final int DEFAULT_SIZE = 2;

    //默认的构造方法
    public MyArrayList(){
        this.elem = new int[DEFAULT_SIZE];
    }

    //指定容量的构造方法
    public MyArrayList(int capacity){
        this.elem = new int[capacity];
    }

    /*
     遍历顺序表中的元素
    */
    @Override
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i]+" ");
        }
        System.out.println();

    }

    @Override
    public boolean isFull() {
        return usedSize == elem.length;
    }

    private void checkCapacity(){
        if (isFull()){
            //扩容
            this.elem = Arrays.copyOf(this.elem, this.elem.length * 2);
        }
    }

    @Override
    public void add(int data) {
        checkCapacity();
        this.elem[this.usedSize++] = data;

    }

    private void checkOnAdd(int pos) throws PosIllegality{
        if (pos < 0 || pos > this.usedSize){
            System.out.println("下标不合法");
            throw new PosIllegality("插入下标异常"+pos);

        }
    }

    @Override
    public void add(int pos, int data) {
        try{
            checkOnAdd(pos);
        }catch (PosIllegality e){
            e.printStackTrace();
            return;
        }
        checkCapacity();
        // 1.从最后一个有效的数据开始往后移
        // 2.当 i<pos 结束
        for (int i = usedSize-1; i >= pos; i--){
            elem[i+1]= elem[i];
        }
        // 3.存放元素到 pos 位置
        elem[pos] = data;
        // 4.usedSize++;
        usedSize++;

    }


    @Override
    public boolean isEmpty() {
        return usedSize == 0;
    }

    @Override
    public boolean contains(int toFind) {
        if (isEmpty()){
            return false;
        }
        for (int i = 0; i < usedSize; i++) {
            // 如果查找引用类型,要重写比较方法
            if (elem[i] == toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        if (isEmpty()){
            return -1;
        }
        for (int i = 0; i < usedSize; i++) {
            // 如果查找引用类型,要重写比较方法
            if (elem[i] == toFind){
                return i;
            }
        }
        return -1;
    }

    private void checkOnGetAndSet(int pos) throws PosIllegality{
        if (pos < 0 || pos >= this.usedSize){
            System.out.println("下标不合法");
            throw new PosIllegality("获取下标元素异常"+pos);

        }
    }

    @Override
    public int get(int pos) throws MyArrayListEmpty{
        checkOnGetAndSet(pos);
        if (isEmpty()){
            throw new MyArrayListEmpty("获取指定下标元素时顺序表为空");
        }
        return elem[pos];
    }

    @Override
    public void set(int pos, int value) {
        checkOnGetAndSet(pos);
        elem[pos] = value;

    }

    @Override
    public void remove(int toRemove) {
        // 1.找到你要删除的数据
        int index = indexOf(toRemove);
        if (index == -1){
            System.out.println("没有这个数字");
            return;
        }
        // 2.挪动数据
        for (int i = index; i < usedSize-1; i++) {
            elem[i] = elem[i+1];
            
        }
        // 3.修改size
        usedSize--;

    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public void clear() {
        // JVM在判断没有人在引用时才会被回收掉,如果数组里面存放的是引用类型,不能直接置为零,会造成内存泄漏
        this.usedSize = 0;


    }


}
